package com.miao.im.biz;

import com.miao.im.common.Message;
import com.miao.im.common.annotation.MessageCommand;
import com.miao.im.common.enums.Command;
import com.miao.im.model.AuthResponseModel;
import com.miao.im.model.HeartBeatModel;
import com.miao.im.scheduler.WsScheduler;
import com.miao.im.server.session.SessionContext;
import com.miao.im.server.session.SessionManager;
import com.miao.im.support.handler.AbstractMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/3/3 16:35
 * @Description:
 */
@Component
@MessageCommand(value = Command.HEART_BEAT)
@Slf4j
public class HeartbeatHandler extends AbstractMessageHandler<HeartBeatModel.Heartbeat> {
    @Autowired
    private WsScheduler wsScheduler;

    @Override
    protected void doRead(HeartBeatModel.Heartbeat body, Message message) {

    }

    @Override
    protected Mono<Void> doDispatch(HeartBeatModel.Heartbeat body, Message message, SessionContext sessionContext) {
        log.info("收到心跳:{}", body.getTimestamp());
        AuthResponseModel.AuthResponse userInfo = sessionContext.getUserInfo();
        //给一个缓冲层，防止频繁写库
        wsScheduler.addWaitingUser(userInfo.getUserId(), userInfo.getDeviceNumber());
//        Message response = Message.writeResponse(1, 0, Command.HEART_BEAT.getCode(), body.toByteArray());
//        SessionManager.send(sessionContext, response);
        return Mono.empty();
    }

}
